﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DelonixRegiaHotel.DBManager;
using DelonixRegiaHotel.Classes;
using System.Collections;

namespace DelonixRegiaHotel
{
    public partial class CreateStaff : Form
    {
        //empty arraylist
        ArrayList aDutyTypeId = new ArrayList();

        public CreateStaff()
        {
            InitializeComponent();
            string staffID = "ST-";
            tbxStaffId.Text = generateID(staffID);

            foreach (Duty d in StaffAndDutyDBManager.GetAllDutyInfo())
            {
                cbDutyTypeID.Items.Add(d.DutyName + "  (" + d.DutyTypeID + ")");
            }
        }

        //generate staff ID
        public string generateID(string id)
        {
            id += DateTime.Now.GetHashCode();
            return id;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string temp = "";
            
          
            //validate for empty fields
            if (tbxFirstName.Text.Equals("") || tbxLastName.Text.Equals("") || tbxNric.Text.Equals("") || tbxAddress.Text.Equals("") || tbxPostalCode.Text.Equals("") || tbxContactNo.Text.Equals("") || tbxEmail.Text.Equals("") || tbxBankAccNo.Text.Equals("") || tbxPassword.Text.Equals("") || tbxHourlyRate.Text.Equals(""))
            {
                MessageBox.Show("Please fill in all the forms");
                return;
            }

            if (cbAccessLvl.SelectedIndex == -1 || cbDutyTypeID.SelectedIndex == -1 || cbShiftType.SelectedIndex == -1 || cbWorkHours.SelectedIndex == -1)
            {
                MessageBox.Show("Ensure that all Access Level, Duty Type and Shift Type have to allocated to Staff");
                return;
            }

            //validate contact no
            if (tbxContactNo.Text.Length != 8)
            {
                System.Windows.Forms.MessageBox.Show("Invalid contact number");
                return;
            }

            //validate date
            DateTime dt;
            if (!DateTime.TryParse(maskedTextBoxDob.Text, out dt))
            {
                MessageBox.Show("Invalid Date");
                return;
            }

            //check selected shiftdays
            //string temp = "";
            if (clbShiftDays.SelectedItem == null)
            {
                MessageBox.Show("Please select shift day(s) for Staff");
                return;
            }
            else
            {
                foreach (Object checkeditem in clbShiftDays.CheckedItems)
                {
                    temp += checkeditem + ", ";
                }
            }
            
            temp = temp.Remove(temp.Length - 2);

            Staff newStaff = new Staff();
            newStaff.Nric = tbxNric.Text;
            newStaff.ShiftDay = temp;
            newStaff.StaffID = tbxStaffId.Text;
            newStaff.FirstName = tbxFirstName.Text;
            newStaff.LastName = tbxLastName.Text;
            newStaff.HomeAddress = tbxAddress.Text;
            newStaff.PostalCode = Convert.ToInt32(tbxPostalCode.Text);
            newStaff.Dob = Convert.ToDateTime(maskedTextBoxDob.Text);
            newStaff.ContactNo = Convert.ToInt32(tbxContactNo.Text);
            newStaff.Email = tbxEmail.Text;
            newStaff.BankAccountNo = Convert.ToInt32(tbxBankAccNo.Text);
            newStaff.Password = tbxPassword.Text;
            newStaff.AccessLevel = Convert.ToInt32(cbAccessLvl.SelectedItem);
            newStaff.ShiftType = Convert.ToString(cbShiftType.SelectedItem);
            newStaff.HourlyRate = Convert.ToDouble(tbxHourlyRate.Text);
            newStaff.WorkHours = Convert.ToInt32(cbWorkHours.SelectedItem);

            // Declare dutyinfo in the arraylist
            aDutyTypeId = StaffAndDutyDBManager.GetAllDutyInfo();
            foreach (Duty d in aDutyTypeId)
            {
                // if selected item in combo box contain the duty type id
                if (cbDutyTypeID.SelectedItem.ToString().Contains(d.DutyTypeID))
                {
                    newStaff.DutyTypeID = d.DutyTypeID;
                }
            }      

             //check if staff already exist based on nric number
            if (StaffAndDutyDBManager.GetStaffByNric(newStaff) == true)
            {
                MessageBox.Show("Staff already exist");
                return;
            }

            else
            {
                int success = StaffAndDutyDBManager.CreateStaff(newStaff);
                if (success == 1)
                {
                    MessageBox.Show("Create Staff successful");

                }
            }

            RefreshForm();

        }         

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RefreshForm()
        {
            string staffID = "ST-";
            tbxStaffId.Text = generateID(staffID);

            tbxFirstName.ResetText();
            tbxLastName.ResetText();
            tbxNric.ResetText();
            tbxAddress.ResetText();
            tbxPostalCode.ResetText();
            maskedTextBoxDob.ResetText();
            tbxContactNo.ResetText();
            tbxEmail.ResetText();
            tbxBankAccNo.ResetText();
            tbxPassword.ResetText();
            cbAccessLvl.ResetText();
            cbShiftType.ResetText();
            cbDutyTypeID.ResetText();
            tbxHourlyRate.ResetText();
            cbWorkHours.ResetText();
        }


        //check for textboxes that only accept interger numbers
        private void tbxPostalCode_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxPostalCode.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void tbxContactNo_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxContactNo.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void tbxBankAccNo_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxBankAccNo.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void tbxHourlyRate_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxHourlyRate.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }
    }
}
